// 首页文字动画
var spans = document.getElementsByTagName('span')
var pageAudio = undefined;
var oldPage = 1;
var oldPageInitData = {
    img: '',
    text: '',
    titlelv1: '',
    titlelv2: '',
    titlelv3: ''
}

function doSetTimeout(i) {
    timeOutList.push(setTimeout(function () {
        spans[i].classList += ' animate-in'
    }, 120 * i));
}

window.addEventListener('load', function () {
    for (var i = 0; i < spans.length; i++) {
        doSetTimeout(i)
    }
})

// 文字和图片切换
var timeOutList = [];
var currentPage = 1;


// 音频播放

function bgPlay() {
    var mediaBG = document.getElementById('audioBG');
    mediaBG.play();
    document.addEventListener("WeixinJSBridgeReady", function () {
        mediaBG.play();
    }, false);
}

function audioAutoPlay(id) {
    var audio = document.getElementById(id);
    audio.play();
    document.addEventListener("WeixinJSBridgeReady", function () {
        audio.play();
    }, false);
    document.addEventListener('YixinJSBridgeReady', function () {
        audio.play();
    }, false);
}


var swiperV = new Swiper('.swiper-container', {
    direction: 'vertical', // 竖着切换
    mousewheel: true, // 鼠标控制
    //设置宽度为全屏  
    width: window.innerWidth,
    height: window.innerHeight,
    // spaceBetween: 50, // 页面间隙
    pagination: {
        el: '.swiper-pagination',
        clickable: true,
    },
    navigation: {
        nextEl: '.swiper-button-next',
    },
    on: {
        init: function () {
            //初始化
            setTimeout(audioAutoPlay('audioBG'), 5000);
            // setTimeout(bgPlay, 5000);
        },
        slideChangeTransitionEnd: function () { // 切换完执行的方法
            var num = this.activeIndex; // 当前是第几个页面
            // ......    // 这里是自己逻辑的代码

            currentPage = num + 1;
            pageLoadInit()

            switch (num) {
                case 1:
                    break;
            }
        },
    },
});

function pageLoadInit() {
    // 停止上一页的声音并清空定时器
    if (pageAudio) {
        pageAudio.currentTime = 0;
        pageAudio.pause();
    }
    timeOutList.forEach(item => {
        clearTimeout(item);
    });
    timeOutList = [];
    // 还原上一页的信息
    if (oldPage != 1) {
        for (let key in oldPageInitData) {
            let id = "#page" + oldPage + key;
            if (key == 'img') {
                $(id).attr("src", oldPageInitData[key]);
            } else {
                $(id).html(oldPageInitData[key]);
            }
        }
    }
    // 将本页的初始信息设置到oldPageInitData
    let tempData = oldPageInitData;
    for (let key in tempData) {
        let id = "#page" + currentPage + key;
        if (key == 'img') {
            oldPageInitData[key] = $(id).attr("src");
        } else {
            oldPageInitData[key] = $(id).html();
        }
    }
    oldPage = currentPage;
}

function pageAnimationLoad(pagedata) {
    // $("#page2img").css("display", "none");

    // 清除所有定时器
    timeOutList.forEach(item => {
        clearTimeout(item)
    });

    // 动画序列
    createAnimationTimer(pagedata.imgList, 'img', currentPage);
    if (currentPage == 2) createAnimationTimer(pagedata.content, 'text', currentPage);
    else if (currentPage > 2) {
        let lazyLoadTime = 0;
        pagedata.textList.forEach(item => {
            lazyLoadTime += text2Load(item, lazyLoadTime)
        });
    }

    // media.play();
}

// 创建动画定时器
function createAnimationTimer(data, type, page, lazyLoadTime) {
    lazyLoadTime = lazyLoadTime ? lazyLoadTime : 0;
    let loadTime = 0;
    let info = {
        id: "#page" + page + type,
        type: type
    };

    data.forEach(item => {
        timeOutList.push(setTimeout(showAndHidden, lazyLoadTime + loadTime, item, info));
        loadTime += item.stayTime;
    });

    return loadTime;
}

// 显示隐藏动画操作序列
function showAndHidden(item, info, intime, outtime) {
    intime = intime ? intime : 500;
    outtime = outtime ? outtime : 500;

    if (info.type == "img") $(info.id).attr("src", item.src);
    else $(info.id).html(item.text);

    if (!item.noFadeIn) $(info.id).fadeIn(intime);
    if (!item.noFadeOut) timeOutList.push(setTimeout(() => {
        $(info.id).fadeOut(outtime)
    }, item.stayTime - intime - outtime));


}

// 递归创建定时器
function text2Load(data, lazyLoadTime) {

    // 本次load耗时
    let loadTime = 0;
    // 递归含有子级的data
    if ('sub' in data) {
        data.sub.forEach(item => {
            loadTime += text2Load(item, lazyLoadTime + loadTime)
        });
    } else if ('content' in data) {
        loadTime += createAnimationTimer(data.content, 'text', currentPage, lazyLoadTime);
    }

    timeOutList.push(setTimeout(() => {
        $("#page" + currentPage + "titlelv" + data.level).html(data.title)
    }, lazyLoadTime));
    return loadTime;
}

$('.pageaudio').bind('play', function () {
    pageAudio = this;
    // currentPage = parseInt($(this).attr("page"));
    pageAnimationLoad(allPageData[currentPage - 2]);
})



